<?php
// use the dPFramework to have easy database operations (store, delete etc.) by using its ObjectOrientedDesign
// therefore we have to create a child class for the module einstein

// a class named (like this) in the form: module/module.class.php is automatically loaded by the dPFramework

/**
 *	@package dotProject
 *	@subpackage modules
 *	@version $Revision: 1.0 $
*/

// include the powerful parent class that we want to extend for einstein
require_once( $AppUI->getSystemClass ('dp' ) );		// use the dPFramework for easy inclusion of this class here

/**
 * The Supplier Class
 */
class CSupplier extends CDpObject {
	// link variables to the einstein object (according to the existing columns in the database table einstein)
	var $sup_id = NULL;	//use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $sup_code = NULL;
	var $sup_name = NULL;
	var $sup_address = NULL;
	var $sup_tel = NULL;
	var $sup_fax = NULL;
	var $sup_vat_code = NULL;
	var $sup_ledger1 = NULL;
	var $sup_ledger2 = NULL;
	var $sup_status = NULL;
	var $sup_type = NULL;
	var $sup_classification = NULL;
	var $sup_division = NULL;
	var $sup_costcenter = NULL;
	var $sup_resource = NULL;
	/*
	ALTER TABLE `suppliers` ADD `sup_exact_code` VARCHAR( 255) ;
	*/
	var $sup_exact_code;

	// the constructor of the CEinstein class, always combined with the table name and the unique key of the table
	function CSupplier() {
		$joins = array(
			array('label'=>'Item',name=>'items', 'idfield'=>'item_id', 'joinfield'=>'item_supplier'),
			array('label'=>'Quotation',name=>'quotations', 'idfield'=>'quote_id', 'joinfield'=>'quote_supplier'),
			array('label'=>'Reference',name=>'projects', 'idfield'=>'project_id', 'joinfield'=>'project_shipper'),
			array('label'=>'Task log',name=>'task_log', 'idfield'=>'task_log_id', 'joinfield'=>'task_log_transporter'),
		);
		$this->CDpObject( 'suppliers', 'sup_id', 'sup_code', $joins, 'suppliers');
	}

	function check() {
		if (!isset($this->_tbl_unique) || $this->_tbl_unique == NULL) {
			return NULL;
		}
		$q = new DBQuery();
		$q->addTable("$this->_tbl");
		if (intval($this->sup_id) > 0) {
			$q->addWhere("$this->_tbl_key != $this->sup_id");
		}
		$q->addWhere("$this->_tbl_unique = '$this->sup_code'");
		$result = $q->loadResult();
		if (isset($result) && $result != '') {
			return 'Duplicate shipper code: ' . $this->sup_code;
		}
		return NULL;
	}

	function getStatusName(){
		//$sql = "select status_name from md_status where status_id = '" . $this->unit_status . "'";
		$q  = new DBQuery;
		$q->addTable('md_status');
		$q->addQuery('status_name');
		$q->addWhere('status_id = '.$this->sup_status);
		$sql = $q->prepare();
		$q->clear();
		$status_name = db_loadResult( $sql );
		return $status_name;
 	}

 	function getTypeName(){
		//$sql = "select status_name from md_status where status_id = '" . $this->unit_status . "'";
		$q  = new DBQuery;
		$q->addTable('md_type');
		$q->addQuery('name');
		$q->addWhere('type_id = '.$this->sup_type);
		$sql = $q->prepare();
		$q->clear();
		$type_name = db_loadResult( $sql );
		return $type_name;
 	}
 	function getCostCenterName(){
 		if($this->sup_costcenter!=null){
			$q  = new DBQuery;
			$q->addTable('costcenters');
			$q->addQuery('cc_name');
			$q->addWhere('cc_id = '.$this->sup_costcenter);
			$sql = $q->prepare();
			$cc_name = db_loadResult( $sql );
			return $cc_name;
 		}
 	}
 	function getClassificationName(){
		//$sql = "select status_name from md_status where status_id = '" . $this->unit_status . "'";
		$q  = new DBQuery;
		$q->addTable('classifications');
		$q->addQuery('cls_name');
		$q->addWhere('cls_id = '.$this->sup_classification);
		$sql = $q->prepare();
		$q->clear();
		$cls_name = db_loadResult( $sql );
		return $cls_name;
 	}

 	function getTypeList(){
 		$result = array('type_id' => 0, 'type_name' => '');
		$q  = new DBQuery;
		$q->addTable('md_type');
		$q->addQuery('type_id, name');
		$q->addWhere('type =\'Supplier\'');
		$sql = $q->prepare();
		$q->clear();
		//db_loadHash($sql, $result);
		return db_loadHashList($sql);
 	}
 	function getClassificationList(){
 		$result = array('cls_id' => 0, 'cls_name' => '');
		$q  = new DBQuery;
		$q->addTable('classifications');
		$q->addQuery('cls_id, cls_name');
		$q->addWhere('cls_type =\'Supplier\'');
		$sql = $q->prepare();
		$q->clear();
		//db_loadHash($sql, $result);
		return db_loadHashList($sql);
 	}

 	function getDivisionList(){
 		$result = array('div_id' => 0, 'div_name' => '');
		$q  = new DBQuery;
		$q->addTable('divisions');
		$q->addQuery('div_id, div_name');
		$sql = $q->prepare();
		$q->clear();
		//db_loadHash($sql, $result);
		return db_loadHashList($sql);
 	}

	function getSupplierList($require = FALSE){
		$q  = new DBQuery;
		$q->addTable('suppliers');
		$q->addQuery('sup_id, sup_name');
		$sql = $q->prepare();
		$q->clear();
		if ($require) {
			return db_loadHashList($sql);
		}
		return arrayMerge(array(''=>''), db_loadHashList($sql));
	}

}

function getSupplierList($require = FALSE) {
	$objSup = new CSupplier();
	return $objSup->getSupplierList($require);
}
?>